// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    datas:[],
    strDesc:wx.consts.txtDesc,
    strTips:'',
  },
  onLoad() {
    this.getConfig()
  },
  getConfig(){
    wx.cloud.callFunction({
      name:'getConfig',
    }).then((res)=>{
      console.log('getConfig',res);
      let bTips = res.result.bTips;
      if (bTips) {
        this.setData({
          strTips : res.result.tips
        })
      } else {
        this.queryVideos()
      }
    })
  },
  queryVideos(){
    const db = wx.cloud.database()
    db.collection('video').limit(100).get().then(res => {
      console.log('xxxxx',res.data)
      if (res.data) {
        // for (let i = 0; i < 20; i++) {
        //   res.data.push(res.data[0])
        // }
        const len = res.data.length;
        const list0 = [];
        const list1 = [];
        const titles = [];
        const path = 'cloud://debug-5gtz5axq72632d3e.6465-debug-5gtz5axq72632d3e-1306892965/'
        res.data.forEach(e => {
          list0.push(path+'video/'+e.name)
          list1.push(path+'cover/'+e.cover)
          titles.push(e.title)
        });

        wx.cloud.getTempFileURL({
          fileList: list0.concat(list1),
          success: res => {
            console.log(res.fileList)
            if (res.fileList) {
              const datas = []
              for (let i = 0; i < len; i++) {
                const e0 = res.fileList[i];
                const e1 = res.fileList[i+len];
                datas[i] = {
                  url : e0.tempFileURL,
                  cover : e1.tempFileURL,
                  // title : titles[i]
                  title : "",
                }
              }
              console.log(datas);
              this.setData({
                datas:datas
              })
            }
          },
          fail: console.error
        })
      }
    })

  },
  clickItem(e){
    getApp().globalData.videos = this.data.datas;
    wx.navigateTo({
      url: '../mypage/mypage?index='+e.mark.index,
    })
  },
  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: '18617958387',
      success() {
        console.log('成功拨打电话')
      }
    })
  },
  makeShare(){
    console.log('share');
    // wx.showShareMenu({
    //   withShareTicket: true,
    //   menus: ['shareAppMessage', 'shareTimeline']
    // })
    wx.showShareMenu({
      success: function () {
          console.log("wx.showShareMenu success");
      },
      fail: function (params) {
          console.log("wx.showShareMenu fail");
          console.log(params);
      }
  });
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  onShareAppMessage() {

  },
  onShareTimeline(){

  },
})
